{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python & 神经网络\n",
    "## Kalman滤波\n",
    "- 简介\n",
    "- 几个模型与应用\n",
    "## Python简易教程\n",
    "- Python简介\n",
    "- 基本数据结构和语法\n",
    "- 面向对象部分\n",
    "- 第三方库介绍\n",
    "## 机器学习简介\n",
    "- 综述与基本概念\n",
    "- 感知机和反向传播\n",
    "- 常用层与Pytorch实践\n",
    "## 作业\n",
    "- 项目框架\n",
    "- 任务"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 安装&环境配置\n",
    "- Anaconda安装/pip\n",
    "  - \n",
    "- pytorch安装\n",
    "- libtorch安装和使用\n",
    "- onnx安装（有时间再说）\n",
    "- openvino安装和配置（同上）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python教程\n",
    "- 数据结构和基本语法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#语法说明：\n",
    "#   4个空格缩进，不要TAB（主要是在非ide里不要TAB）\n",
    "#   没有；\n",
    "#   不要显示声明变量类型\n",
    "#   变量命名和c++基本一致，_\\a-z\\数字，_和数字不能在第一个\n",
    "#动态推断，不需要显式声明类型\n",
    "# 字符串操作：+/spilt/upper/lower\n",
    "a = \"faf\"\n",
    "b = \"123{}\"\n",
    "c = a+\"_\"+b.format(\"p\")\n",
    "c.split(\"_\")\n",
    "# int和float->类似于c，但是精度理论是无限的\n",
    "# list列表->类似于vector,但是不会限制数据类型\n",
    "d = [\"da\",1,3]\n",
    "d.append(\"d\")\n",
    "# 访问方式：d[0]/d[-1]\n",
    "# 类方法\n",
    "\n",
    "# 字典dict->键值对\n",
    "e = {\"key\":\"value\"}\n",
    "# 访问方式e[\"keys\"]\n",
    "\n",
    "#元组tuple,类似于list，但长度和值不能改变\n",
    "f = (1,3,4)\n",
    "#集合set/frozenset\n",
    "g = {1,2,3}\n",
    "\n",
    "# 循环\n",
    "#while循环\n",
    "i = 0\n",
    "while 1:\n",
    "    i +=1\n",
    "    if (i>5):\n",
    "        break\n",
    "#continue和break\n",
    "#for循环->可迭代对象，用法range\\list\\tuple \n",
    "for i in range(10):\n",
    "    print(i)\n",
    "# 判断\n",
    "if (a == b):\n",
    "    print(\"a = b\")\n",
    "elif (c==d):\n",
    "    print(\"c==d\")\n",
    "else:\n",
    "    pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'123456'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 函数\n",
    "def function(x:float,y=21,*args,**kwargs):\n",
    "    \"\"\"\n",
    "    Brief:\n",
    "      参数\n",
    "      关键字参数\n",
    "      缺省参数\n",
    "      docstring\n",
    "    \"\"\"\n",
    "    return x+y\n",
    "\n",
    "# 类和对象->对象是类的一个实例\n",
    "# object基类\n",
    "#self关键字\n",
    "#__init__特殊方法\n",
    "#属性和类方法\n",
    "class Human(object):\n",
    "    def __init__(self,id:int):\n",
    "        self.id = id\n",
    "    \n",
    "    def reid(self,new_id):\n",
    "        self.id = new_id\n",
    "\n",
    "#继承\n",
    "#super关键字\n",
    "class Student(Human):\n",
    "    def __init__(self,id,sid):\n",
    "        super(Student,self).__init__(id)\n",
    "        self.sid = sid\n",
    "\n",
    "    def __str__(self):\n",
    "        return str(self.id)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 3)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#模块和包\n",
    "#导入 import\n",
    "import numpy as np \n",
    "#import cv2\n",
    "#from xxx import *\n",
    "#相对导入\n",
    "\n",
    "# 其他操作\n",
    "#if __name__ == \"__main__\"\n",
    "\n",
    "#第三方库\n",
    "#numpy\n",
    "#https://www.runoob.com/numpy/numpy-array-manipulation.html\n",
    "data = np.array([[1,2,3],[4,5,6]])#array数组\n",
    "a2 = np.random.rand(4,3,12,12)\n",
    "#数据类型\n",
    "a2.dtype()\n",
    "data.astype(np.float)\n",
    "#索引操作\n",
    "a2[:,:,3:5,:-3]\n",
    "#reshape\n",
    "#np.reshape\n",
    "#增加维度\n",
    "np.expand_dims(data,0)\n",
    "#减少一个维度\n",
    "np.squeeze(data,0)\n",
    "data.shape\n",
    "#add:+\n",
    "#dot:np.dot,*不是矩阵乘法\n",
    "#数组拼接\n",
    "#np.concatenate()\n",
    "\n",
    "#只有\n",
    "#opencv-python\n",
    "#import cv2\n",
    "#img = cv2.imread(\"./test.png\",0)\n",
    "#读取黑白图像,np.array存储\n",
    "#matplotlib\n",
    "\n",
    "#torch\n",
    "#张量\n",
    "import torch\n",
    "tensor = torch.Tensor([[1,2,3]])\n",
    "#从numpy中获取 \n",
    "tensor2 = torch.from_numpy(a2)\n",
    "#转换为numpy\n",
    "tensor2.numpy()\n",
    "#张量类型\n",
    "tensor2.dtype\n",
    "#类型转换\n",
    "tensor2.int()\n",
    "tensor2.to(torch.device(\"cpu\"))\n",
    "#张量大小\n",
    "tensor.shape\n",
    "#reshape\n",
    "tensor.view()\n",
    "#浅拷贝，共享内存\n",
    "tensor.reshape()\n",
    "#张量是否存储在连续内存区域\n",
    "tensor.is_contiguous\n",
    "#深copy\n",
    "#tensor.clone()\n",
    "#维度变化\n",
    "#torch.unsqueeze()\n",
    "#torch.squeeze()\n",
    "#拼接\n",
    "#torch.cat()\n",
    "#索引操作与numpy基本一致\n",
    "#运算：\n",
    "#https://zhuanlan.zhihu.com/p/138596554"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 机器学习\n",
    "### 综述\n",
    "- 分类\n",
    "    - 监督学习\n",
    "    - 无监督学习\n",
    "    - 强化学习\n",
    "\n",
    "- CV方向应用\n",
    "    - 分类\n",
    "    - 目标检测(Object Detection)\n",
    "    - 目标跟踪\n",
    "    - 语义分割\n",
    "    - 视频理解\n",
    "    - 人体姿态\n",
    "\n",
    "- 基本概念（部分）\n",
    "    - 张量(Tensor)\n",
    "    - 计算图\n",
    "    - M-P神经元模型\n",
    "    - 损失函数和泛化\n",
    "    - 训练集和交叉验证集\n",
    "    - 过拟合和欠拟合\n",
    "    - 数据预处理和数据增强\n",
    "\n",
    "### 感知机和反向传播算法\n",
    "- 单层感知机\n",
    "- 多层感知机\n",
    "- 反向传播算法和trick"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 常见层和Pytorch实践\n",
    "- 深度学习框架\n",
    "    - Tensorflow\n",
    "    - Pytorch \n",
    "- Tensor操作\n",
    "- 常见层 \n",
    "    - Conv2D：2d卷积\n",
    "        卷积操作过程->卷积神经网络\n",
    "    - 池化操作->降采样\n",
    "    - 批量归一化(BN)\n",
    "    - 全连接层\n",
    "    - Dropout\n",
    "    - Concat：深度融合\n",
    "    - Add: 值相加\n",
    "- Pytorch实现过程\n",
    "    - DataLoader\n",
    "    - 模型构建nn.Module和nn.Sequential\n",
    "    - 训练过程\n",
    "    - 交叉验证\n",
    "    - 模型导出和使用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 项目框架介绍：\n",
    "- 数据集结构\n",
    "    - 按照类名创建文件夹\n",
    "    - *.names是各类对应的编码\n",
    "- requirements.txt\n",
    "  - 依赖项，这是我自己的版本，但是这几个包是必须要装的，版本除了pytorch其他基本没啥要求，python版本我一般用3.6(18.04预装)\n",
    "  - torch和torchvision的版本存在对应关系，导出onnx版本也和torch版本有关\n",
    "- gen_dataset_list.py\n",
    "    - 用于生成train.txt和valid.txt ,每个record为\"filename(绝对路径) label\"\n",
    "    - 使用:\"python3 gen_dataset_list.py --root-dir \\home\\hitcrt\\dataset\\num --output-dir  \\home\\hitcrt\\dataset\\num\" \\\n",
    "    root-dir为数据集根目录，output-dir为*.txt 的输出路径\n",
    "- dataset.py\n",
    "  - ImagerRecord类\n",
    "  - Dataset类\n",
    "    - get用于预处理\n",
    "    - `__getitem__()`用于实现DataLoader的特殊方法\n",
    "    - `__len__()`\n",
    "- model.py\n",
    "    - 创建模型，继承nn.Module类实现\n",
    "    - nn.Sequential()类构建模型\n",
    "    - 直接运行导出libtorch模型\n",
    "- opts.py\n",
    "    - 解析命令行，各个参数\n",
    "    - 如何在main函数中使用以及实例属性\n",
    "- eval.py:\n",
    "  - 没啥用，可以自己从checkpoint里保存模型然后自己测试一下\n",
    "- train.py\n",
    "  - 核心训练python文件\n",
    "  - main函数流程：\n",
    "    - 解析命令行——>args\n",
    "    - 初始化模型和模型参数\n",
    "    - 训练初始化和主循环(train函数和validate函数)\n",
    "  - train()\n",
    "    - train()模式\n",
    "    - 加载dataloader\n",
    "    - forward + loss\n",
    "    - backword + 清除梯度\n",
    "  - validate()\n",
    "    - eval()\n",
    "    - forward + loss\n",
    "- run_train.sh\n",
    "  - 如何调用python文件的脚本\n",
    "\n",
    "- 如何可视化模型？\n",
    "  - torchsummary—>命令行下\n",
    "  - netron->一般需要导出为onnx\n",
    "\n",
    "- 需要完成那些部分？\n",
    "  - model.py->创建模型\n",
    "  - dataset.py的Dataset.get()方法\n",
    "  - eval.py的test()预处理部分需要自己完成,如果需要可视化，建议matplotlib，会matlab就会\n",
    "  - train.py:模型初始化(权重初始化可以不需要完成)、训练初始化、train()和valid(),accuary()和评价相关随意\n",
    "  - libtorch导出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
